package com.suncnpap.datamanage.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.app.util.common.UuidUtil;
import com.suncnpap.library.model.ChineseClassification;

@Component
public class DataImpUtil {
	
	private Logger log = Logger.getLogger(DataImpUtil.class);
	
	@Value("${spring.datasource.driver-class-name}")
	private String driver;
	
	@Value("${spring.datasource.url}")
	private String url; 
	
	@Value("${spring.datasource.username}")
	private String user;
	
	@Value("${spring.datasource.password}")
	private String password;
	
	private Connection conn =  null;
	
	
	public void init() {
		this.conn = createConnection();
		
    }
	
	/**
	 * 建立一个连接
	 * @return
	 */
	public Connection createConnection() {		
			try {
				Class.forName(driver);
				Connection con = DriverManager.getConnection(url,user, password);
				return con;
			} catch (ClassNotFoundException e) {
				log.error("驱动未找到", e);
				return null;
			} catch (SQLException e) {
				log.error("建立连接失败", e);
				return null;
			}
	}
	
	/**
	 * 释放资源
	 */
	public void release(){
		if (this.conn != null ) {
			try {
				this.conn.close();
				System.out.println("连接资源释放成功！");
			} catch (SQLException e) {
				log.error("释放资源失败", e);
			}
		}
	}

	public List<ChineseClassification> initTableData(String strCreatId, String strCreatName, String strSchoolId) {
		
		List<ChineseClassification> list = new ArrayList<ChineseClassification>();
    	
		try{
 		String sql2=" select * from t_default_chineseclassification order by strCode asc";
 		PreparedStatement ps2=conn.prepareStatement(sql2);
 		ResultSet rs= ps2.executeQuery(); 
 		Map<String, ChineseClassification> map = new HashMap<String, ChineseClassification>();
 		
 		if (rs != null && rs.next()) {
            do {
            	ChineseClassification obj = new ChineseClassification();
            	
            	obj.setStrId(UuidUtil.get32UUID());
            	
            	obj.setStrCode(rs.getString("strCode"));
            	obj.setStrNodeName(rs.getString("strNodeName"));
            	
            	obj.setEXT_1(rs.getString("strId"));
            	obj.setEXT_2(rs.getString("strParentNodeId"));
            	if(map.containsKey(obj.getEXT_2())){
            		ChineseClassification parent = map.get(obj.getEXT_2());
            		obj.setStrParentNodeName(parent.getStrNodeName());
                	obj.setStrParentNodeId(parent.getStrId());
            	}else{
            		obj.setStrParentNodeName(rs.getString("strParentNodeName"));
                	obj.setStrParentNodeId(rs.getString("strParentNodeId"));
            	}
            	obj.setDtCreatDate(new Date());
            	obj.setStrCreatId(strCreatId);
            	obj.setStrCreatName(strCreatName);
            	obj.setStrSchoolId(strSchoolId);
            	
            	map.put(obj.getEXT_1(), obj);
            	list.add(obj);
            	
            } while (rs.next());
        }

		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}finally{
			release();		
		}
		return list;
	}
	
	

	
	
	//依据“时间戳+随机数”生成ID
    public static String getID() {
        return "" + new Date().getTime() + Math.round(Math.random()*8999+1000);
    }
}
